{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 下载h5文件特征数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/malele/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4784, 30)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/malele/anaconda3/lib/python3.6/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.\n",
      "  from numpy.core.umath_tests import inner1d\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import h5py\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import metrics\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.svm import SVC\n",
    "\n",
    "file = h5py.File('DB3//DB3_S1_feature.h5','r')\n",
    "featureData   = file['featureData'][:]\n",
    "featureLabel  = file['featureLabel'][:]\n",
    "\n",
    "file.close()\n",
    "featureData = MinMaxScaler().fit_transform(featureData) # 缩放到[0, 1]\n",
    "train_x, test_x, train_y, test_y = train_test_split(featureData, featureLabel, test_size=0.1)\n",
    "\n",
    "print(featureData.shape)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 原始数据训练 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RF train accuracy: 100.00%\n",
      "RF test  accuracy: 89.35%\n",
      "training took 3.064731s!\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "start_time = time.time()\n",
    "\n",
    "RF = RandomForestClassifier(n_estimators=200, criterion='gini', max_depth=None, min_samples_split=2, \n",
    "                            min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features='auto',\n",
    "                            max_leaf_nodes=None, min_impurity_decrease=0.0, min_impurity_split=None, \n",
    "                            bootstrap=True, oob_score=True, n_jobs=1, random_state=None, verbose=0,\n",
    "                            warm_start=False, class_weight=None)\n",
    "\n",
    "\n",
    "RF.fit(train_x, train_y)\n",
    "score = RF.score(train_x, train_y)\n",
    "predict = RF.predict(test_x)\n",
    "accuracy = metrics.accuracy_score(test_y, predict)\n",
    "\n",
    "print(\"RF train accuracy: %.2f%%\" %(100*score))\n",
    "print('RF test  accuracy: %.2f%%' % (100 * accuracy))\n",
    "print ('training took %fs!' % (time.time() - start_time))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9407665505226481 0.9081419624217119\n"
     ]
    }
   ],
   "source": [
    "SVM=SVC(C=2, kernel='rbf', degree=3, gamma=2)\n",
    "SVM.fit(train_x, train_y)\n",
    "\n",
    "score=SVM.score(train_x, train_y)\n",
    "predict_SVM  = SVM.predict(test_x)\n",
    "accuracy_SVM  = metrics.accuracy_score(test_y, predict_SVM)\n",
    "print(score,accuracy_SVM)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
